/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SongResourcesTable.java
 *
 * Created on May 25, 2010, 7:59:54 PM
 */

package org.dafe.partyband.gui.imports;

import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;
import org.dafe.partyband.db.api.ISongResource;
import org.dafe.partyband.gui.Utilities;
import org.dafe.partyband.gui.Utilities.CustomColumnFactory;
import org.dafe.partyband.importer.api.IMimeTypeDetector;
import org.dafe.partyband.importer.api.ISongImport;
import org.dafe.partyband.importer.api.ISongImport.IEvaluation;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;
import org.jdesktop.swingx.util.OS;
import org.openide.util.NbBundle;

/**
 *
 * @author dafe
 */
public class SongResourcesTable extends javax.swing.JPanel {

    private ISongImport songImport;
    private final IMimeTypeDetector mimeDetector;

    private static final String[] columnNames = new String[] { "doImport", "material" };
    
    private static final Class[] types = new Class [] {
        java.lang.Boolean.class, java.lang.String.class
    };


    /** Creates new form SongResourcesTable */
    public SongResourcesTable(IMimeTypeDetector mimeDetector) {
        this.mimeDetector = mimeDetector;

        initComponents();

        resourcesTable.addHighlighter(HighlighterFactory.createAlternateStriping());

        // define column features and propertiesHINT_ImportMaterialColumn
        CustomColumnFactory customCF = new Utilities.CustomColumnFactory();
        CustomColumnFactory.ColumnDescription importCheckboxDesc = new CustomColumnFactory.ColumnDescription();
        importCheckboxDesc.title = "";
        importCheckboxDesc.tooltip = NbBundle.getMessage(SongResourcesTable.class, "HINT_ImportMaterialColumn");
        importCheckboxDesc.prototypeDescription = new CustomColumnFactory.PrototypeDescription("  ", 1.5f, 2);
        importCheckboxDesc.cellEditor = new JXTable.BooleanEditor();
        customCF.setColumnDescription(columnNames[0], importCheckboxDesc);

        CustomColumnFactory.ColumnDescription materialDesc = new CustomColumnFactory.ColumnDescription();
        materialDesc.title = NbBundle.getMessage(NameAuthorTable.class, "TIT_MaterialColumn");
        materialDesc.tooltip = NbBundle.getMessage(SongResourcesTable.class, "HINT_MaterialColumn");
        customCF.setColumnDescription(columnNames[1], materialDesc);

        resourcesTable.setColumnFactory(customCF);

        // workaround for https://swingx.dev.java.net/issues/show_bug.cgi?id=1297
        if (OS.isLinux()) {
            UIManager.put("Table.focusSelectedCellHighlightBorder",
                    BorderFactory.createEmptyBorder(0, 0, 0, 0));
            UIManager.put("Table.focusCellHighlightBorder",
                    BorderFactory.createEmptyBorder(0, 0, 0, 0));
        }
    }

    public void setSongImport (ISongImport songImport, boolean enabled) {
        this.songImport = songImport;
        resourcesTable.setModel(new ResourcesModel());
        resourcesTable.setEnabled(enabled);
        jXTitledSeparator1.setTitle(NbBundle.getMessage(SongResourcesTable.class,
                "SongResourcesTable.jXTitledSeparator1.title", // NOI18N
                songImport.getSongToImport().getName()));
        /*if (enabled) {
            resourcesTable.removeHighlighter(grayHighlighter);
        } else {
            resourcesTable.addHighlighter(grayHighlighter);
        }*/
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXTitledSeparator1 = new org.jdesktop.swingx.JXTitledSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        resourcesTable = new org.jdesktop.swingx.JXTable();

        setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 2, 0, 2));

        jXTitledSeparator1.setToolTipText(org.openide.util.NbBundle.getMessage(SongResourcesTable.class, "SongResourcesTable.jXTitledSeparator1.toolTipText")); // NOI18N
        jXTitledSeparator1.setTitle(org.openide.util.NbBundle.getMessage(SongResourcesTable.class, "SongResourcesTable.jXTitledSeparator1.title", new Object[] {"---"})); // NOI18N

        resourcesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        resourcesTable.setShowHorizontalLines(false);
        resourcesTable.setShowVerticalLines(false);
        resourcesTable.setSortable(false);
        jScrollPane1.setViewportView(resourcesTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(161, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXTitledSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTitledSeparator jXTitledSeparator1;
    private org.jdesktop.swingx.JXTable resourcesTable;
    // End of variables declaration//GEN-END:variables

    private class ResourcesModel extends AbstractTableModel {

        List<ISongResource> resources = songImport.getSongToImport().getResources();
        IEvaluation evaluation = songImport.getEvaluation();

        public int getRowCount() {
            return resources.size();
        }

        public int getColumnCount() {
            return 2;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            ISongResource curRes = resources.get(rowIndex);
            if (columnIndex == 0) {
                return evaluation.isImportEnabled(curRes);
            } else {
                String mime = curRes.getMimeType();
                return mimeDetector.getMimeDisplayName(mime) + " (" + mime + ")";
            }
        }

        @Override
        public Class getColumnClass(int columnIndex) {
            return types[columnIndex];
        }

        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex == 0;
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                ISongResource curRes = resources.get(rowIndex);
                evaluation.setImportEnabled(curRes, (Boolean) aValue);
            }
        }

    }

}
